package com.bookmngsys.controller;

import com.bookmngsys.View.NotificationDto;
import com.bookmngsys.dao.BorrowHistoryDao;
import com.bookmngsys.dao.FineHistoryDao;
import com.bookmngsys.dao.ReservationHistoryDao;
import com.bookmngsys.entity.BorrowBook;
import com.bookmngsys.entity.BorrowHistory;
import com.bookmngsys.entity.FineHistory;
import com.bookmngsys.entity.ReservationHistory;
import com.bookmngsys.utils.Response;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Random;

@Controller
@RequestMapping(value = "/api/notification")
public class NotificationController {
    @Autowired
    BorrowHistoryDao borrowHistoryDto;
    @Autowired
    ReservationHistoryDao reservationHistoryDao;
    @Autowired
    FineHistoryDao fineHistoryDao;

    @RequestMapping(method = RequestMethod.GET, path = "/getOverdueNum")
    @ResponseBody
    @ApiOperation("获取用户的超期图书数目")
    Response getOverdueNum(@RequestAttribute int userId){
        try{
            NotificationDto notificationDto = new NotificationDto();
            List<BorrowHistory> borrowHistoryList = borrowHistoryDto.getAllBorrowHistoryByReaderId(userId);
            for(BorrowHistory a: borrowHistoryList){
                if(a.getIsReturned() != null && a.getIsReturned() == true){
                    continue;
                }
                if(System.currentTimeMillis() > a.getReturnDate()){
                    notificationDto.setNum(notificationDto.getNum() + 1);
                }
            }
            return new Response().success(notificationDto);
        }catch (Exception e){
            return new Response().failure(e.getMessage());
        }
    }

    @RequestMapping(method = RequestMethod.GET, path = "/getBookWillReturnNum")
    @ResponseBody
    @ApiOperation("获取用户的即将归还的图书数目，当前日期距离归还日期不足1天，即为即将需要归还的图书")
    Response getBookWillReturnNum(@RequestAttribute int userId){
        long oneDay = 24 * 3600 * 1000;
        try{
            NotificationDto notificationDto = new NotificationDto();
            List<BorrowHistory> borrowHistoryList = borrowHistoryDto.getAllBorrowHistoryByReaderId(userId);
            for(BorrowHistory a: borrowHistoryList){
                if(a.getIsReturned() != null && a.getIsReturned() == true){
                    continue;
                }
                long cur = System.currentTimeMillis();
                if(cur <= a.getReturnDate() && cur + oneDay > a.getReturnDate()){
                    notificationDto.setNum(notificationDto.getNum() + 1);
                }
            }
            return new Response().success(notificationDto);
        }catch (Exception e){
            return new Response().failure(e.getMessage());
        }
    }

    @RequestMapping(method = RequestMethod.GET, path = "/getReservationWillInvalidNum")
    @ResponseBody
    @ApiOperation("获取用户的预约即将失效的数目，当前日期距离预约结束时间不足1小时，即为即将失效的预约")
    Response getReservationWillInvalidNum(@RequestAttribute int userId){
        long oneHour = 3600 * 1000;
        try{
            NotificationDto notificationDto = new NotificationDto();
            System.out.println(userId);
            List<ReservationHistory> reservationHistories = reservationHistoryDao.getAllReservationHistoryByReaderId(userId);
            for(ReservationHistory a: reservationHistories){
                if(a.getIsUsed() != null && a.getIsUsed() == true){
                    continue;
                }
                long cur = System.currentTimeMillis();
                long reverseDueDate = a.getReserveDate() + 4 * oneHour;
                if(cur <= reverseDueDate &&  cur + oneHour > reverseDueDate){
                    notificationDto.setNum(notificationDto.getNum() + 1);
                }
            }
            System.out.println(notificationDto.getNum());
            return new Response().success(notificationDto);
        }catch (Exception e){
            return new Response().failure(e.getMessage());
        }
    }
}
